package com.caldremch.component.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.support.annotation.StringRes;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.caldremch.component.R;
import com.caldremch.component.listner.NavigationBarListner;


/**
 * Created by Caldremch on 2017-10-19 15:27
 * <p>
 * 顶部导航栏 支持定制
 */

public class NavigationBar extends RelativeLayout implements View.OnClickListener {

    public Context mContext;
    public NavigationBarListner mNavigationBarListner;

    public void setListner(NavigationBarListner mNavigationBarListner) {
        this.mNavigationBarListner = mNavigationBarListner;
    }


    public NavigationBar(Context context) {
        this(context, null);
    }

    public NavigationBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public NavigationBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        init(context);
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.NavigationBar);


        String title = ta.getString(R.styleable.NavigationBar_title);
        String leftTitle = ta.getString(R.styleable.NavigationBar_leftTitle);
        String rightImg = ta.getString(R.styleable.NavigationBar_rightImg);

        Drawable leftDrawable = ta.getDrawable(R.styleable.NavigationBar_leftImg);
        Drawable rightDrawable = ta.getDrawable(R.styleable.NavigationBar_rightImg);

        //这里获取的时候是像素, 注意了
        int mLeftTextSize = ta.getDimensionPixelSize(R.styleable.NavigationBar_leftTitleTextSize, getResources().getDimensionPixelSize(R.dimen.default_title_size));
        int mRightTextSize = ta.getDimensionPixelSize(R.styleable.NavigationBar_rightTitleTextSize, getResources().getDimensionPixelSize(R.dimen.default_title_size));
        int mTitleTextSize = ta.getDimensionPixelSize(R.styleable.NavigationBar_titleTextSize, getResources().getDimensionPixelSize(R.dimen.default_title_size));

        int mLeftTextColor = ta.getColor(R.styleable.NavigationBar_leftTitleColor, getResources().getColor(R.color.titleColor));
        int mRightTextColor = ta.getColor(R.styleable.NavigationBar_rightTitleColor, getResources().getColor(R.color.titleColor));
        int mTitleTextColor = ta.getColor(R.styleable.NavigationBar_titleColor, getResources().getColor(R.color.titleColor));
        int mHeaderBg = ta.getColor(R.styleable.NavigationBar_backgroundColor, getResources().getColor(R.color.default_head_bg));

        mHeader.setBackgroundColor(mHeaderBg);

        //字体大小设置已经转换为像素设置
        mLeftText.setTextSize(TypedValue.COMPLEX_UNIT_PX, mLeftTextSize);
        mRightText.setTextSize(TypedValue.COMPLEX_UNIT_PX, mRightTextSize);
        mTitleText.setTextSize(TypedValue.COMPLEX_UNIT_PX, mTitleTextSize);

        mTitleText.setText(title);
        mLeftText.setText(leftTitle);
        mRightText.setText(rightImg);


        mTitleText.setTextColor(mTitleTextColor);
        mLeftText.setTextColor(mLeftTextColor);
        mRightText.setTextColor(mRightTextColor);


        if (leftDrawable != null) {
            leftDrawable.setBounds(0, 0, leftDrawable.getMinimumWidth(), leftDrawable.getMinimumHeight());
            mLeftText.setCompoundDrawables(leftDrawable, null, null, null);
        }

        if (rightDrawable != null) {
            rightDrawable.setBounds(0, 0, rightDrawable.getMinimumWidth(), rightDrawable.getMinimumHeight());
            mLeftText.setCompoundDrawables(rightDrawable, null, null, null);
        }

        ta.recycle();

    }


    private TextView mTitleText;
    private TextView mLeftText;
    private TextView mRightText;
    private RelativeLayout mHeader;


    private void init(Context context) {
        LayoutInflater.from(context).inflate(R.layout.layout_title, this);
        mTitleText = (TextView) findViewById(R.id.tv_title_center);
        mLeftText = (TextView) findViewById(R.id.tv_title_left);
        mRightText = (TextView) findViewById(R.id.tv_title_right);
        mHeader = (RelativeLayout) findViewById(R.id.header);
        mTitleText.setOnClickListener(this);
        mLeftText.setOnClickListener(this);
        mRightText.setOnClickListener(this);
    }

    public void setTile(String text) {
        mTitleText.setText(text);
    }
    public void setTile(@StringRes int text) {
        mTitleText.setText(getContext().getString(text));
    }

    public TextView getLeftView() {
        return mLeftText;
    }

    public TextView getRightView() {
        return mRightText;
    }

    public void showLeftText(boolean isShow) {
        if (isShow) {
            mLeftText.setVisibility(VISIBLE);
        } else {
            mLeftText.setVisibility(GONE);
        }
    }

    public void showRightText(boolean isShow) {
        if (isShow) {
            mRightText.setVisibility(VISIBLE);
        } else {
            mRightText.setVisibility(GONE);
        }
    }

    public void setLeftImage(int leftImage) {
        Drawable drawable = getResources().getDrawable(leftImage);
        if (drawable != null) {
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            mLeftText.setCompoundDrawables(drawable, null, null, null);
        }
    }

    public void setRightImage(int leftImage) {
        Drawable drawable = getResources().getDrawable(leftImage);
        if (drawable != null) {
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            mRightText.setCompoundDrawables(null, null, drawable, null);
        }
    }

    public void setLeftTextTitle(String leftTitle) {
        mLeftText.setText(leftTitle);
    }


    public void setTitle(String title) {
        mTitleText.setText(title);
    }

    public void setRightTextTitle(String title) {
        mRightText.setText(title);
    }
    public void setRightTextTitle(@StringRes int text) {
        mRightText.setText(getContext().getString(text));
    }



    //Android library中生成的R.java中的资源ID不是常数,
    //打开library中的R.java，发现确实如此，每一个资源ID都没有被声明为final：
    //switch中 case 后面跟的都是常数,所以要把 switch 换位 if 即可
    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.tv_title_left) {
            if (mNavigationBarListner != null) {
                mNavigationBarListner.onLeftClick(v);
            }

        } else if (i == R.id.tv_title_center) {
            if (mNavigationBarListner != null) {
                mNavigationBarListner.onTitleClick(v);
            }

        } else if (i == R.id.tv_title_right) {
            if (mNavigationBarListner != null) {
                mNavigationBarListner.onRightClick(v);
            }

        }
    }

}


